package com.luli.test;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import org.attoparser.ParseException;

import static com.luli.Utils.DateUtils.StrToDate;

/**
 * 基于Java8 分组再统计
 * @author zzg
 *
 */
public class JavaDemo {

    static List<Fruit> initDate(){
        List<Fruit> list = new ArrayList<Fruit>();

        Fruit one = new Fruit();
        one.setName("苹果一级");
        one.setSid("1");
        one.setPrice(new BigDecimal("123456.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        one.setTotal(1100L);
        one.setDate(StrToDate("20220201"));


        Fruit two = new Fruit();
        two.setName("苹果二级");
        two.setSid("2");
        two.setPrice(new BigDecimal("123546.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        two.setTotal(89L);
        two.setDate(StrToDate("20220203"));

        Fruit three = new Fruit();
        three.setName("苹果三级");
        three.setSid("3");
        three.setPrice(new BigDecimal("987.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        three.setTotal(1039L);
        three.setDate(StrToDate("20220204"));


        Fruit four = new Fruit();
        four.setName("梨子一级");
        four.setSid("5");
        four.setPrice(new BigDecimal("97.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        four.setTotal(39L);
        four.setDate(StrToDate("20220205"));

        Fruit five = new Fruit();
        five.setName("梨子二级");
        five.setSid("5");
        five.setPrice(new BigDecimal("970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        five.setTotal(399L);
        five.setDate(StrToDate("20220206"));

        Fruit six = new Fruit();
        six.setName("西瓜一级");
        six.setSid("5");
        six.setPrice(new BigDecimal("1970.98").setScale(BigDecimal.ROUND_HALF_UP, 2) );
        six.setTotal(2399L);
        six.setDate(StrToDate("20220207"));

        list.add(one);
        list.add(two);
        list.add(three);
        list.add(four);
        list.add(five);
        list.add(six);
        return list;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        List<Fruit> list = initDate();
        //分组
//        Map<String, List<Fruit>> collect1 = list.stream()
//                .collect(Collectors.groupingBy(Fruit::getSid))
//                ;
//        System.out.println( collect1.entrySet());
//        for (Map.Entry<String, List<Fruit>> entry : collect1.entrySet()) {
//            System.out.println("分组" + JSONUtil.toJsonStr(entry));
//        }
//
//
//
//        //分组求和
//        Map<String, LongSummaryStatistics> collect = list.stream().collect(
//                Collectors.groupingBy(Fruit::getSid,
//                        Collectors.summarizingLong(Fruit::getTotal)));
//        System.out.println(collect);
//        for (Map.Entry<String, LongSummaryStatistics> entry : collect.entrySet()) {
//            System.out.println(" 11111  "+ entry.getValue());
//            LongSummaryStatistics longSummaryStatistics = entry.getValue();
//            System.out.println("----------------key----------------" + entry.getKey());
//            System.out.println("求和:" + longSummaryStatistics.getSum());
//            System.out.println("求平均" + longSummaryStatistics.getAverage());
//            System.out.println("求最大:" + longSummaryStatistics.getMax());
//            System.out.println("求最小:" + longSummaryStatistics.getMin());
//            System.out.println("求总数:" + longSummaryStatistics.getCount());
//        }

        List<Date> dateList = Lists.newArrayList();
        for (Fruit item : list){
            dateList.add(item.getDate());
        }
        if (!CollectionUtils.isEmpty(dateList))
            System.out.println(Collections.max(dateList));
    }

    public static Date convert(String s) {
        Date date = null;
        //实现将字符串转成⽇期类型
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            date = dateFormat.parse(s);
        } catch (java.text.ParseException e) {
            throw new RuntimeException(e);
        }
        return date;
    }


    static class Fruit{
        private String sid;
        private String name;
        private Date Date;
        private Long total;
        private BigDecimal price;
        public String getSid() {
            return sid;
        }
        public void setSid(String sid) {
            this.sid = sid;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public Date getDate() {
            return Date;
        }
        public void setDate(Date date) {
            this.Date = date;
        }
        public Long getTotal() {
            return total;
        }
        public void setTotal(Long total) {
            this.total = total;
        }
        public BigDecimal getPrice() {
            return price;
        }
        public void setPrice(BigDecimal price) {
            this.price = price;
        }
    }

}